/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJInternalFrame.java
 *
 * Created on 18/06/2013, 06:45:27 PM
 */
package pe.edu.unmsm.cinfo.siga.ui.ventanas;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collections;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;
import org.jdesktop.observablecollections.ObservableCollections;
import pe.edu.unmsm.cinfo.siga.datos.Precio;
import pe.edu.unmsm.cinfo.siga.datos.Programa;
import pe.edu.unmsm.cinfo.siga.datos.ProgramaModulo;

/**
 *
 * @author alumno
 */
public class GestionarPreciosXModuloInternalFrame extends javax.swing.JInternalFrame {
   //constructors
   /** Creates new form NewJInternalFrame */
   public GestionarPreciosXModuloInternalFrame() {
      initComponents();
   }
   //methods
   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
   @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

      entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacionSIGAPU").createEntityManager();
      org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getResourceMap(GestionarPreciosXModuloInternalFrame.class);
      progQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery(resourceMap.getString("progQuery.query")); // NOI18N
      progList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : progQuery.getResultList();
      moduloSeleccionado = new pe.edu.unmsm.cinfo.siga.datos.ProgramaModulo();
      cancelButton = new javax.swing.JButton();
      grabarButton = new javax.swing.JButton();
      programaComboBox = new javax.swing.JComboBox();
      moduloComboBox = new javax.swing.JComboBox();
      scrollTabla = new javax.swing.JScrollPane();
      tabla = new javax.swing.JTable();
      txtPrograma = new javax.swing.JLabel();
      txtModulo = new javax.swing.JLabel();

      setClosable(true);
      setIconifiable(true);
      setMaximizable(true);
      setTitle(resourceMap.getString("Form.title")); // NOI18N
      setName("Form"); // NOI18N

      javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(pe.edu.unmsm.cinfo.siga.ui.Main.class).getContext().getActionMap(GestionarPreciosXModuloInternalFrame.class, this);
      cancelButton.setAction(actionMap.get("cancelButtonActionPermormed")); // NOI18N
      cancelButton.setIcon(resourceMap.getIcon("cancelButton.icon")); // NOI18N
      cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
      cancelButton.setName("cancelButton"); // NOI18N

      grabarButton.setAction(actionMap.get("grabarButtonActionPermormed")); // NOI18N
      grabarButton.setIcon(resourceMap.getIcon("grabarButton.icon")); // NOI18N
      grabarButton.setText(resourceMap.getString("grabarButton.text")); // NOI18N
      grabarButton.setName("grabarButton"); // NOI18N

      programaComboBox.setAction(actionMap.get("programaComboBoxActionPerformed")); // NOI18N
      programaComboBox.setName("programaComboBox"); // NOI18N

      org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, progList, programaComboBox);
      bindingGroup.addBinding(jComboBoxBinding);

      moduloComboBox.setAction(actionMap.get("moduloComboBoxActionPerformed")); // NOI18N
      moduloComboBox.setName("moduloComboBox"); // NOI18N
      moduloComboBox.setRenderer(new pe.edu.unmsm.cinfo.siga.ui.utils.ModuloListCellRenderer());

      org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedItem.programaModuloList}");
      jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, programaComboBox, eLProperty, moduloComboBox);
      jComboBoxBinding.setSourceNullValue(Collections.EMPTY_LIST);
      bindingGroup.addBinding(jComboBoxBinding);
      org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado}"), moduloComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
      bindingGroup.addBinding(binding);

      //dies

      scrollTabla.setName("scrollTabla"); // NOI18N

      tabla.setName("tabla"); // NOI18N
      tabla.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
      tabla.getTableHeader().setResizingAllowed(false);
      tabla.getTableHeader().setReorderingAllowed(false);

      eLProperty = org.jdesktop.beansbinding.ELProperty.create("${moduloSeleccionado.modulo.precioList}");
      org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, tabla);
      org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${alumnoTipo.alutCod}"));
      columnBinding.setColumnName("Alumno Tipo.alut Cod");
      columnBinding.setColumnClass(Integer.class);
      columnBinding.setEditable(false);
      columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${alumnoTipo.alutDesc}"));
      columnBinding.setColumnName("Alumno Tipo.alut Desc");
      columnBinding.setColumnClass(String.class);
      columnBinding.setEditable(false);
      columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${precMont}"));
      columnBinding.setColumnName("Prec Mont");
      columnBinding.setColumnClass(String.class);
      jTableBinding.setSourceNullValue(Collections.EMPTY_LIST);
      jTableBinding.setSourceUnreadableValue(Collections.EMPTY_LIST);
      bindingGroup.addBinding(jTableBinding);
      jTableBinding.bind();
      scrollTabla.setViewportView(tabla);
      tabla.getColumnModel().getColumn(0).setResizable(false);
      tabla.getColumnModel().getColumn(0).setPreferredWidth(15);
      tabla.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tabla.columnModel.title1")); // NOI18N
      tabla.getColumnModel().getColumn(1).setResizable(false);
      tabla.getColumnModel().getColumn(1).setPreferredWidth(300);
      tabla.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tabla.columnModel.title0")); // NOI18N
      tabla.getColumnModel().getColumn(2).setResizable(false);
      tabla.getColumnModel().getColumn(2).setPreferredWidth(50);
      tabla.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tabla.columnModel.title2")); // NOI18N

      txtPrograma.setText(resourceMap.getString("txtPrograma.text")); // NOI18N
      txtPrograma.setName("txtPrograma"); // NOI18N

      txtModulo.setText("Modulo:"); // NOI18N
      txtModulo.setName("txtModulo"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
               .addGroup(layout.createSequentialGroup()
                  .addComponent(scrollTabla, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                  .addContainerGap())
               .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                     .addComponent(txtPrograma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .addComponent(txtModulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                  .addGap(18, 18, 18)
                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addComponent(programaComboBox, 0, 391, Short.MAX_VALUE)
                     .addComponent(moduloComboBox, 0, 391, Short.MAX_VALUE))
                  .addGap(47, 47, 47))
               .addGroup(layout.createSequentialGroup()
                  .addGap(134, 134, 134)
                  .addComponent(grabarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(38, 38, 38)
                  .addComponent(cancelButton)
                  .addContainerGap())))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(txtPrograma, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addComponent(programaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(txtModulo)
               .addComponent(moduloComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(scrollTabla, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(grabarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
      );

      bindingGroup.bind();

      pack();
   }// </editor-fold>//GEN-END:initComponents
   @Action
   public void grabarButtonActionPermormed() {
      try{
         entityManager.getTransaction().begin();
         for(Precio precio : getModuloSeleccionado().getModulo().getPrecioList()){
            Precio aGuardar = entityManager.find(Precio.class, precio.getPrecioPK());
            entityManager.merge(aGuardar);
         }
         entityManager.getTransaction().commit();
         JOptionPane.showMessageDialog(this, "La lista de precios ha sido guardada...!");
      }catch(Exception e){
         JOptionPane.showMessageDialog(this, e.getMessage() + "...!");
         e.printStackTrace();
         entityManager.getTransaction().rollback();
      }
   }
   @Action
   public void cancelButtonActionPermormed() {
      try {
         dispose();
      }catch(Exception e){
         JOptionPane.showMessageDialog(this, e.getMessage() + "...!");
      }
   }
   //attributes
   private PropertyChangeSupport changeSupport;
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton cancelButton;
   private javax.persistence.EntityManager entityManager;
   private javax.swing.JButton grabarButton;
   private javax.swing.JComboBox moduloComboBox;
   private pe.edu.unmsm.cinfo.siga.datos.ProgramaModulo moduloSeleccionado;
   private java.util.List<Programa> progList;
   private javax.persistence.Query progQuery;
   private javax.swing.JComboBox programaComboBox;
   private javax.swing.JScrollPane scrollTabla;
   private javax.swing.JTable tabla;
   private javax.swing.JLabel txtModulo;
   private javax.swing.JLabel txtPrograma;
   private org.jdesktop.beansbinding.BindingGroup bindingGroup;
   // End of variables declaration//GEN-END:variables

   public ProgramaModulo getModuloSeleccionado() {
      return moduloSeleccionado;
   }

   public void setModuloSeleccionado(ProgramaModulo moduloSeleccionado) {
      ProgramaModulo old = this.moduloSeleccionado;
      moduloSeleccionado.getModulo().setPrecioList(ObservableCollections.observableList(moduloSeleccionado.getModulo().getPrecioList()));
      this.moduloSeleccionado = moduloSeleccionado;
      getChangeSupport().firePropertyChange("moduloSeleccionado", old, moduloSeleccionado);
   }

   public PropertyChangeSupport getChangeSupport() {
      if (changeSupport == null) {
         changeSupport = new PropertyChangeSupport(this);
      }
      return changeSupport;
   }
   @Override
   public void addPropertyChangeListener(PropertyChangeListener l) {
      getChangeSupport().addPropertyChangeListener(l);
   }
   @Override
   public void removePropertyChangeListener(PropertyChangeListener l) {
      getChangeSupport().removePropertyChangeListener(l);
   }
}
